"""
简历分析Agent
分析简历内容，提取技能、优势、改进建议等
"""
from rag_system.retriever import RAGRetriever
from .qwen_client import QWENClient

class AnalyzerAgent:
    def __init__(self, rag_retriever: RAGRetriever):
        self.rag_retriever = rag_retriever
        self.qwen_client = QWENClient()

    def analyze_resume(self, resume_content: str):
        try:
            # 使用通义千问进行智能分析
            analysis = self.qwen_client.analyze_resume_skills(resume_content)
            
            # 结合RAG检索的技能信息
            rag_skills = self.rag_retriever.retrieve_skill_matches(resume_content, n_results=10)
            rag_skill_names = [s['content'] for s in rag_skills]
            
            # 合并分析结果
            result = {
                'technical_skills': analysis.get('technical_skills', []),
                'soft_skills': analysis.get('soft_skills', []),
                'rag_skills': rag_skill_names,
                'skill_level': analysis.get('skill_level', '未知'),
                'strengths': analysis.get('strengths', []),
                'improvement_suggestions': analysis.get('improvement_suggestions', [])
            }
            
            return result
            
        except Exception as e:
            # 如果DeepSeek调用失败，使用备用方法
            return self._fallback_analysis(resume_content)
    
    def _fallback_analysis(self, resume_content: str):
        # 备用分析方法
        skills = self.rag_retriever.retrieve_skill_matches(resume_content, n_results=10)
        strengths = [s['content'] for s in skills[:3]]
        common_skills = {'Python', 'JavaScript', 'Vue.js', 'React', 'MySQL', 'Git'}
        resume_words = set(resume_content.split())
        missing = list(common_skills - resume_words)
        advice = f"建议补充以下常见技能：{'、'.join(missing)}" if missing else "技能覆盖较全面"
        
        return {
            'technical_skills': [s['content'] for s in skills],
            'soft_skills': [],
            'rag_skills': [s['content'] for s in skills],
            'skill_level': '未知',
            'strengths': strengths,
            'improvement_suggestions': [advice]
        }
